<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mis_tech_support1 extends MY_Controller {
	
	function __construct() {
		parent::__construct(); 
		
		$this->load->model('mis_tech_support1_model', 'INVENTORY_PERANGKAT');
		$this->secure_session();
	}

	function index() {
		$data['title'] = "Penugasan Teknisi";
		$data['ext_kondisi']=$this->INVENTORY_PERANGKAT->GetJob();
		viewByTemplate('mis_tech_support1/index', $data, 'grid');
	}

	function getdata() {
		$this->secure_session();
		echo $this->INVENTORY_PERANGKAT->GetJSON();
		
	}

	function ajax_post() {
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try {
			if (count( $error->Get() ) > 0) { 
				$is_error = 1; 
			} else {
				// Fill Post Data
				$data	= post_by_key(array('kodebarang', 'namabarang', 'tipe', 'harga', 'jumlah', 'kodekondisi', 'keterangan'));
				$data['kodebarang']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['kodebarang'])));
				
				if (post("mode") == "create") {
					//$data['tgl_rekam']	= NOW;
					//$data['user_rekam']	= USERID;

					$this->INVENTORY_PERANGKAT->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.app_id ?? ""));

				} else if (post("mode") == "edit") {
					//$data['tgl_ubah']	= NOW;
					//$data['user_ubah']	= USERID;
					
					$this->INVENTORY_PERANGKAT->Update($data, 
										array('kodebarang'	=> $data['kodebarang']));

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS002", "edit/" + (data.app_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	private function validation() {
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation
		if (post("kodebarang") == '') {
			$error->Add("kodebarang", "Kode Barang", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM ADMIN_PRIMKOKAS.EXT_BARANG WHERE KODEBARANG = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("KODEBARANG"))))."'")) > 0) {
			$error->Add("kodebarang", "Kode Barang", $tpl_duplicate);
		}

		if (post("namabarang") == '') {
			$error->Add("namabarang", "Nama Barang", $tpl_empty);
		}

		return $error;
	}

	function ajax_delete() {
		$ls_result	= "";
		$kodebarang	= get('kodebarang');
		
		if ($kodebarang != "") {
			// Delete Single
			$arr_rule	= array('kodebarang' => $kodebarang);
			$ls_result = $this->INVENTORY_PERANGKAT->DeleteSingle($arr_rule);

		} else {
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++) {
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('kodebarang' => $row['kodebarang']);
				$ls_result .= $this->INVENTORY_PERANGKAT->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}

}
?>